---
title: Add unique constraint
description: Add unique operations add a `UNIQUE` constraint to a column.
---

## Structure

<YamlJsonTabs>
```yaml
alter_column:
  table: table name
  column: column name
  unique:
    name: name of unique constraint
  up: SQL expression
  down: SQL expression
```
```json
{
  "alter_column": {
    "table": "table name",
    "column": "column name",
    "unique": {
      "name": "name of unique constraint"
    },
    "up": "SQL expression",
    "down": "SQL expression"
  }
}
```
</YamlJsonTabs>

Use the `up` SQL expression to migrate values from the old non-unique column in the old schema to the `UNIQUE` column in the new schema.

## Examples

### Add a `UNIQUE` constraint

Add a `UNIQUE` constraint to the `review` column in the `reviews` table. The `up` SQL appends a random suffix to ensure uniqueness:

<ExampleSnippet example="15_set_column_unique.yaml" languange="yaml" />
